package ru.csat.jmsmonitor;

import com.google.gson.Gson;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hornetq.api.core.management.MessageCounterInfo;
import org.hornetq.api.core.management.ObjectNameBuilder;
import org.hornetq.api.jms.management.JMSQueueControl;

import javax.jms.Queue;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: A.Senatorov
 * Date: 19.11.12
 * Time: 16:48
 * To change this template use File | Settings | File Templates.
 */
public class Task implements Runnable {
    private static Log log = LogFactory.getLog(Main.class);

    @Override
    public void run() {
        Context initialContext = null;
        JMXConnector connector = null;

        try {
            initialContext = new InitialContext(Service.getEnv());
            Queue queue = (Queue) initialContext.lookup("/queue/" + Service.getProperties().getProperty("queue.name"));
            ObjectName on = ObjectNameBuilder.DEFAULT.getJMSQueueObjectName(queue.getQueueName());
            connector = JMXConnectorFactory.connect(new JMXServiceURL(Service.getProperties().getProperty("jmx_url")), new HashMap());
            MBeanServerConnection mbsc = connector.getMBeanServerConnection();
            JMSQueueControl queueControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, JMSQueueControl.class, false);

            String counters = queueControl.listMessageCounter();
            MessageCounterInfo messageCounter = MessageCounterInfo.fromJSON(counters);

            int count = messageCounter.getDepth();
            if (count < Integer.valueOf(Service.getProperties().getProperty("warning_msg_count"))) {
                log.info("jms queue length is " + count + ". Additional info: " + new Gson().toJson(messageCounter));
            }

            if (count >= Integer.valueOf(Service.getProperties().getProperty("warning_msg_count"))
                    && count < Integer.valueOf(Service.getProperties().getProperty("fatal_msg_count"))) {
                log.warn("jms queue length is " + count + ". Additional info: " + new Gson().toJson(messageCounter));
            }

            if (count >= Integer.valueOf(Service.getProperties().getProperty("fatal_msg_count"))) {
                log.fatal("jms queue length is " + count + ". Additional info: " + new Gson().toJson(messageCounter));
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
            try {
                connector.close();
                initialContext.close();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }
}
